package adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.sxpjyy.monkey.mobilenurse.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import objects.OrderDetail;

/**
 * Created by Monkey on 2017/6/22.
 * 医嘱执行适配器
 */

public class OrdersExecuteAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private LayoutInflater inflater;
    private Map<OrderDetail, List<OrderDetail>> dataSets;
    private List<OrderDetail> parentsList;

    public OrdersExecuteAdapter(Context context) {
        this.mContext = context;
        this.inflater = LayoutInflater.from(mContext);
    }

    public void setDataSets(Map<OrderDetail, List<OrderDetail>> dataSets) {
        this.dataSets = dataSets;
        parentsList = new ArrayList<>();
        getParentList();
        Collections.sort(parentsList, new Comparator<OrderDetail>() {
            @Override
            public int compare(OrderDetail arg0, OrderDetail arg1) {
                int a = arg0.getJHSJ().compareTo(arg1.getJHSJ());
                if (a != 0) {
                    return a;
                } else {
                    return arg0.getJHH().compareTo(arg1.getJHH());
                }
            }
        });

    }

    private void getParentList() {
        Set set = dataSets.keySet();
        for (Object aSet : set) {
            parentsList.add((OrderDetail) aSet);
        }
    }

    public List<OrderDetail> getChildList(int groupPosition) {
        if (parentsList != null && parentsList.get(groupPosition) != null && dataSets.get(parentsList.get(groupPosition)) != null) {
            return dataSets.get(parentsList.get(groupPosition));
        }
        return null;
    }

    @Override
    public int getGroupCount() {
        if (parentsList != null && parentsList.size() > 0) {
            return parentsList.size();
        }
        return 0;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (parentsList != null && parentsList.get(groupPosition) != null && dataSets.get(parentsList.get(groupPosition)) != null) {
            return dataSets.get(parentsList.get(groupPosition)).size();
        }
        return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        if (parentsList != null && parentsList.get(groupPosition) != null) {
            return parentsList.get(groupPosition);
        }
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if (parentsList != null && dataSets.get(parentsList.get(groupPosition)) != null) {
            return dataSets.get(parentsList.get(groupPosition)).get(childPosition);
        }
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ParentViewHolder holder;
        if (convertView == null) {
            holder = new ParentViewHolder();
            convertView = inflater.inflate(R.layout.item_oe_parent, parent, false);
            holder.oe_yzmc = (TextView) convertView.findViewById(R.id.oe_yzmc_text);
            holder.oe_yzzt = (TextView) convertView.findViewById(R.id.oe_yzzt_text);
            holder.oe_jhsj = (TextView) convertView.findViewById(R.id.oe_jhsj_text);
            holder.oe_ypyf = (TextView) convertView.findViewById(R.id.oe_ypyf_text);
            holder.oe_sypc = (TextView) convertView.findViewById(R.id.oe_sypc_text);
            holder.oe_ycjl = (TextView) convertView.findViewById(R.id.oe_ycjl_text);
            holder.oe_ycsl = (TextView) convertView.findViewById(R.id.oe_ycsl_text);
            convertView.setTag(holder);
        } else {
            holder = (ParentViewHolder) convertView.getTag();
        }
        if (groupPosition % 2 == 0) {
            convertView.setBackgroundResource(R.drawable.lv_even_line);
        } else {
            convertView.setBackgroundResource(R.drawable.lv_odd_line);
        }

        if (parentsList != null && parentsList.size() > 0) {
            OrderDetail parentYZJH = parentsList.get(groupPosition);
            String yzmc = parentYZJH.getYZMC() == null ? "" : parentYZJH.getYZMC();
            holder.oe_yzmc.setText(yzmc);
            String yzzt = "";
            int color = mContext.getResources().getColor(R.color.colorBlack);
            switch (parentYZJH.getYZZT()) {
                case "0":
                    yzzt = "未执行";
                    color = mContext.getResources().getColor(R.color.colorRed);
                    break;
                case "1":
                    yzzt = "已执行";
                    color = mContext.getResources().getColor(R.color.colorBlue);
                    break;
                case "2":
                    yzzt = "执行中";
                    color = mContext.getResources().getColor(R.color.colorBlue);
                    break;
                case "4":
                    yzzt = "暂停中";
                    color = mContext.getResources().getColor(R.color.colorBlue);
                    break;
                case "5":
                    yzzt = "拒绝";
                    color = mContext.getResources().getColor(R.color.colorRed);
                    break;
                case "6":
                    yzzt = "取消";
                    color = mContext.getResources().getColor(R.color.colorRed);
                    break;
            }
            holder.oe_yzzt.setText(yzzt);
            holder.oe_yzzt.setTextColor(color);
            String jhsj = parentYZJH.getJHSJ() == null ? "" : parentYZJH.getJHSJ().substring(5, 16);
            holder.oe_jhsj.setText(jhsj);
            String ypyf = parentYZJH.getYFMC() == null ? "" : parentYZJH.getYFMC();
            holder.oe_ypyf.setText(ypyf);
            String sypc = parentYZJH.getSYPC() == null ? "" : parentYZJH.getSYPC();
            holder.oe_sypc.setText(sypc);
            String ycsl = parentYZJH.getYCSL() + " " + parentYZJH.getSLDW();
            holder.oe_ycsl.setText(ycsl);
            String ycjl = parentYZJH.getYCJL() + " " + parentYZJH.getJLDW();
            holder.oe_ycjl.setText(ycjl);
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder holder;
        if (convertView == null) {
            holder = new ChildViewHolder();
            convertView = inflater.inflate(R.layout.item_oc_child, parent, false);
            holder.oe_yzmc = (TextView) convertView.findViewById(R.id.oc_yzmc_text);
            holder.oe_ycjl = (TextView) convertView.findViewById(R.id.oc_ycjl_text);
            holder.oe_ycsl = (TextView) convertView.findViewById(R.id.oc_ycsl_text);
            convertView.setTag(holder);
        } else {
            holder = (ChildViewHolder) convertView.getTag();
        }
        if (groupPosition % 2 == 0) {
            convertView.setBackgroundColor(mContext.getResources().getColor(R.color.even_bg));
        } else {
            convertView.setBackgroundColor(mContext.getResources().getColor(R.color.odd_bg));
        }
        List<OrderDetail> childList = dataSets.get(parentsList.get(groupPosition));
        if (childList != null && childList.size() > 0) {
            OrderDetail childYZJH = childList.get(childPosition);
            String yzmc = childYZJH.getYZMC() == null ? "" : childYZJH.getYZMC();
            holder.oe_yzmc.setText(yzmc);
            String ycsl = childYZJH.getYCSL() + " " + childYZJH.getSLDW();
            holder.oe_ycsl.setText(ycsl);
            String ycjl = childYZJH.getYCJL() + " " + childYZJH.getJLDW();
            holder.oe_ycjl.setText(ycjl);
        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private class ParentViewHolder {
        TextView oe_yzmc;
        TextView oe_yzzt;
        TextView oe_jhsj;
        TextView oe_ypyf;
        TextView oe_sypc;
        TextView oe_ycjl;
        TextView oe_ycsl;
    }

    private class ChildViewHolder {
        TextView oe_yzmc;
        TextView oe_ycjl;
        TextView oe_ycsl;
    }
}
